package com.example.administrator.xxnapp.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.xxnapp.R;

import butterknife.BindView;
import butterknife.ButterKnife;

//

/**
 * Created by Administrator on 2018/4/23.
 */

public class TitleBarLayout extends RelativeLayout{
    @BindView(R.id.title_bar_layout_IV_raw)
    ImageView mIVRaw;
    @BindView(R.id.title_bar_layout_TV_title)
    TextView mTVTitle;
    @BindView(R.id.title_bar_layout_RL_root)
    View mViewRoot;
    private boolean showRaw;
    private String title;
    private int backgroundRes;

    public TitleBarLayout(Context context) {
        this(context,null);
    }

    public TitleBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TitleBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.title_bar_layout,this);
        ButterKnife.bind(this);
        initAttrs(context,attrs);
        mIVRaw.setVisibility(showRaw?VISIBLE:GONE);
        mTVTitle.setText(TextUtils.isEmpty(title)?"":title);
        mViewRoot.setBackgroundResource(backgroundRes);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.TitleBarLayout);
        showRaw = array.getBoolean(R.styleable.TitleBarLayout_showRaw,true);
        title = array.getString(R.styleable.TitleBarLayout_title);
        backgroundRes = array.getResourceId(R.styleable.TitleBarLayout_backgroundRes,R.color.base_color);
        array.recycle();
    }
}
